using System;
using Sustainalytics.Synchronization.Validation.Internals;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Common
{

    public static class RulesExtensions
    {
        public static bool Validator(IComparable modelValue, IComparable referenceValue,IComparable From, IComparable To)
        {
            if (modelValue == null || referenceValue == null)
            {
                throw new ValidationException("Unable to cast to a comparable value");
            }


            if (
                !(modelValue is decimal || referenceValue is decimal || modelValue is double || referenceValue is double
                  || modelValue is float || referenceValue is float || modelValue is int || referenceValue is int))
            {
                throw new ValidationException("Unable to cast to a comparable value");
            }

            var percent = Math.Abs(Convert.ToDouble(modelValue) - Convert.ToDouble(referenceValue))
                          / Convert.ToDouble(referenceValue);

            if (To == null)
            {
                if (Comparer.GetComparisonResult(percent, From) < 0)
                {
                    return false;
                }
            }
            else
            {
                if (Comparer.GetComparisonResult(percent, From) < 0 || Comparer.GetComparisonResult(percent, To) >= 0)
                {
                    return false;
                }
            }

            return true;
        }
    }

}